package com.ll.aync;

import com.alibaba.fastjson.JSONObject;
import com.ll.TestAround;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.scheduling.annotation.Async;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ContextRefreshedApplicationListenerDemo {
    public static void main(String[] args) throws InterruptedException {
//		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//		applicationContext.register(ContextRefreshedListener.class);
//		applicationContext.register(EventListenerConfig.class);
////		applicationContext.register(CustomEventListener.class);
//		applicationContext.register(TestAround.class);
//		applicationContext.register(AspectJProxyFactory.class);
//		applicationContext.refresh();
//		System.out.println(Thread.currentThread().getName() +  "  主方法线程 ");
//
//		ApplicationListener bean = (ApplicationListener) applicationContext.getBean("customEventListener");
//		AspectJProxyFactory factory = (AspectJProxyFactory) applicationContext.getBean("aspectJProxyFactory");
//		TestAround testAround = (TestAround) applicationContext.getBean("testAround");
//		factory.addAspect(testAround);
////		TestAround testAround = new TestAround();
////		factory.addAspect(testAround);
//
//		CustomEvent customEvent = new CustomEvent("自定义事件");
//		CountDownLatch latch =  new CountDownLatch(1);
//		customEvent.count = latch;
//		applicationContext.publishEvent(customEvent);
//		try {
//			latch.await(10L,TimeUnit.SECONDS);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//		Service service = new Service();
//		for (int i = 0; i< 100; i++){
//			new Thread(() ->{
//				System.out.println(service.isHandlerStatus());
//			}).start();
//		}
        int i = 1;
        try {
            Boolean s = null;
            if(s);
        } catch (Exception e) {
            i =5;
            throw e;
        } finally {
            System.out.println("ssss" + i);
        }

//		System.out.println(JSONObject.toJSONString("asdasd"));
    }
}


class Service {
    private boolean handlerStatus = true;

//	public Service(CountDownLatch latch) {
//		this.latch = latch;
//	}


//	@Async
//	public void exec() {
//		try {
//			System.out.println(Thread.currentThread().getName() + " execute task. ");
//			sleep(2);
//			System.out.println(Thread.currentThread().getName() + " finished task. ");
//		} finally {
//			latch.countDown();
//		}
//	}

    public boolean isHandlerStatus() {
        if (!this.handlerStatus) {
            return false;
        }
        synchronized (Service.class) {
            if (this.handlerStatus) {
                this.handlerStatus = false;
                return true;
            }
            return false;
        }

    }

    private void sleep(int seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
